﻿unit LoadTXT;



interface
  uses
    SysUtils,
    Classes,
    Points,
    SplitString,
    SortByASC,
    SplitByTStringList;

procedure  ReadTxt(FileName : string);

procedure ReadTxtByTSL(FileName : string);

implementation

//3 使用TStringList分隔 然后 TList排序
procedure ReadTxtByTSL(FileName : string);
var
  List : TList;
  I : Integer;
  MyPoint : PPoint;
begin
  List := TList.Create;
  SplitByTSL(List,FileName);

  //调用排序
  List.Sort(Compare);
//
  //遍历输出List:TList
  for I := 0 to List.Count - 1 do
  begin
    MyPoint := List.Items[I];
    Writeln(MyPoint.X,' ',MyPoint.Y);
  end;
  Readln;

  for I := 0 to List.Count - 1 do
  begin
    Dispose(List.Items[I]);
  end;

  List.Free;
  //Dispose(MyPoint);
end;


procedure  ReadTxt(FileName : string);
var
  F : TextFile;
  Str : string;
  StrList : TStringList;
  List : TList;
  MyPoint : PPoint;
  //ArrPoint : Array of TPoint;   //1
  I : Integer;

begin
  //读取文件
  AssignFile(F,FileName);
  Reset(F);              //只读文件
  StrList := TStringList.Create;
  List := TList.Create;
  while not EOF(F) do
  begin
    ReadLn(F,Str);
    StrList.Add(Str);
  end;
  CloseFile(F);

  //1 设置动态数组长度，将StrList每行按 逗号 分隔之后加入到数组当中
  //SetLength(ArrPoint,StrList.Count);    //1
  for I := 0 to StrList.Count - 1 do
  begin
    //2.调用分隔字符串的方法，在方法里为指针分配了动态空间
    MyPoint :=  SplitStrP(StrList[I],',');
    //ArrPoint[I].X := MyPoint.X;      //1
    //ArrPoint[I].Y := MyPoint.Y;      //1
    List.Add(MyPoint);
    //释放指针空间
    Dispose(MyPoint);
  end;

  //2.调用排序
  List.Sort(Compare);

  //2. 遍历输出List:TList
  for I := 0 to List.Count - 1 do
  begin
    MyPoint := List.Items[I];
    Writeln(MyPoint.X,' ',MyPoint.Y);
  end;

  //1.对数组排序，然后输出每个元素
//  Sort(ArrPoint);
//  StrList.Free;
//  for I := Low(ArrPoint) to High(ArrPoint) do
//  begin
//    Writeln(ArrPoint[I].X,' ',ArrPoint[I].Y);
//  end;
  Readln;

  StrList.Free;
  List.Free;
end;

end.
